﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AMVCommander
{
    public partial class FormEditItem : Form
    {
        bool bOk = false;
        public String Value;
        string EditValue;
        bool bEdit { get { return EditValue != ""; } }

        string[] ItemL;
        string[] ExistL;

        public FormEditItem()
        {
            InitializeComponent();
        }

        public bool ShowDialog(string EditValue, string[] ExistL, string[] AllItemL, string Caption)
        {
            this.EditValue = EditValue;
            this.ExistL = ExistL;
            this.ItemL = AllItemL;
            bFill = true;
            txb.Text = EditValue;
            bFill = false;
            RefillList();
            bOk = false;

            ShowDialog();
            return bOk;
        }

        void RefillList()
        {
            lb.Items.Clear();
            string str = txb.Text.Trim().ToUpper();
            foreach (string s in ItemL)
            {
                if (s.Trim().ToUpper().Contains(str))
                    lb.Items.Add(s);
            }
        }

        private void txb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.IsInputKey = false;
                // проверки
                string s = txb.Text.Trim();
                if (s == "") { MessageBox.Show("Поле не может быть пустым"); return; }
                if (!CheckName(s)) { MessageBox.Show("Такое значение уже существует"); return; }
                Value = s;
                bOk = true;
                Close();
            }

            if (e.KeyCode == Keys.Escape) { e.IsInputKey = false; Close(); }

        }

        bool CheckName(String s)
        {
            string S = s.ToUpper();
            if (bEdit && EditValue.ToUpper() == S) return true;

            foreach (string str in ExistL)
                if (str.ToUpper() == S)
                    return false;

            return true;
        }

        private void FormEditMusic_Shown(object sender, EventArgs e)
        {
            //
        }

        bool bFill = false;
        private void txb_TextChanged(object sender, EventArgs e)
        {
            if (bFill) return;
            RefillList();
        }

        private void lb_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(lb.SelectedIndex==-1)return;
            bFill = true;
            txb.Text = lb.SelectedItem.ToString();
            bFill = false;

            // проверки
            string s = txb.Text.Trim();
            if (s == "") { return; }
            if (!CheckName(s)) {return; }

            Value = s;
            bOk = true;
            Close();
        }

        private void lb_MouseDown(object sender, MouseEventArgs e)
        {
            /*if (lb.SelectedIndex == -1) return;
            txb.Text = lb.SelectedItem.ToString();*/
        }

    }
}
